/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.uct.client  
 */
package com.apache.rpc.client;

import java.util.HashMap;
import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.rpc.client.protocol.AbstractProtocolClient;
import com.apache.rpc.client.protocol.HttpProtocolServiceClient;
import com.apache.rpc.client.protocol.HttpsProtocolServiceClient;
import com.apache.rpc.client.protocol.SocketProtocolServiceClient;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.ConfigUtil;

/**
 * description:  用户中心客户端协议调用工具
 * @author dwu 创建时间：2015-3-31  
 * @Copyright(c)2014:
 */
public class RpcProtoclClient {

	private static RpcProtoclClient instance;
	private AbstractProtocolClient httpService;
	private AbstractProtocolClient httpsService;
	private AbstractProtocolClient socketService;

	private RpcProtoclClient() {
		httpService = new HttpProtocolServiceClient();
		httpsService = new HttpsProtocolServiceClient();
		socketService = new SocketProtocolServiceClient();
	}

	public static RpcProtoclClient getInstance() {
		if (null == instance) {
			instance = new RpcProtoclClient();
		}
		return instance;
	}

	/**
	 * description:  获取接口调用信息
	 * @param beanId  访问的接口调用Spring beanId
	 * @param methodKey 方法标识
	 * @param params   调用接口的方法所属的方法参数
	 * @return   ResultEntity对象
	 */
	public ResultEntity doService(String beanId, String methodKey, Map<String, Object> params) {
		String protocolType = ConfigUtil.getInstance().getValueByKey("protocol_type");
		if ("".equals(protocolType) || null == protocolType || "null".equalsIgnoreCase(protocolType)) {
			protocolType = "socket";
		}
		InterfaceRegister register = getInterfaceRegister(beanId, methodKey);
		if ("socket".equals(protocolType)) {
			socketService.setRegister(register);
			return socketService.doService(beanId, methodKey, params);
		} else if ("http".equals(protocolType)) {
			httpService.setRegister(register);
			return httpService.doService(beanId, methodKey, params);
		} else {
			httpsService.setRegister(register);
			return httpsService.doService(beanId, methodKey, params);
		}
	}

	/**
	 * description:  获取接口调用信息
	 * @param beanId  访问的接口调用Spring beanId
	 * @param methodKey 方法标识
	 * @param params   调用接口的方法所属的方法参数
	 * @param register 接口注册信息
	 * @return   ResultEntity对象
	 */
	public ResultEntity doService(String beanId, String methodKey, Map<String, Object> params,
			InterfaceRegister register) {
		if (null == register)
			return null;
		String protocolType = register.getCallType();
		if ("socket".equals(protocolType)) {
			socketService.setRegister(register);
			return socketService.doService(beanId, methodKey, params);
		} else if ("http".equals(protocolType)) {
			httpService.setRegister(register);
			return httpService.doService(beanId, methodKey, params);
		} else {
			httpsService.setRegister(register);
			return httpsService.doService(beanId, methodKey, params);
		}
	}

	private InterfaceRegister getInterfaceRegister(String beanId, String methodKey) {
		String rpcIp = ConfigUtil.getInstance().getValueByKey("rpc_service_ip");
		String rpcPort = ConfigUtil.getInstance().getValueByKey("rpc_service_port");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("beanId", beanId);
		params.put("methodKey", methodKey);
		InterfaceRegister ir = new InterfaceRegister();
		ir.setAddress(rpcIp);
		ir.setPort(rpcPort);
		socketService.setRegister(ir);
		ResultEntity resultEntity = socketService.doService("rpcService", "selectByBeanId", params);
		if ("true".equals(resultEntity.getResult())) {
			if (null != resultEntity.getEntity())
				return (InterfaceRegister) resultEntity.getEntity();
		}
		return null;
	}
}
